package com.goldenautumn.prefabricate.activity.category

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter4.BaseMultiItemAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.business.entity.GoodsInfoVO
import com.goldenautumn.prefabricate.databinding.ItemGoodsGridLayoutBinding
import com.goldenautumn.prefabricate.databinding.ItemGoodsLineLayoutBinding
import com.goldenautumn.prefabricate.databinding.ItemHomeGoodsBinding
import com.goldenautumn.prefabricate.databinding.ItemHotSearchBinding
import com.goldenautumn.prefabricate.utils.loadImgUrl

class CategoryLayoutAdapter : BaseMultiItemAdapter<GoodsInfoVO>() {

    companion object {
        private const val ITEM_TYPE_LINE = 1
        private const val ITEM_TYPE_GRID = 2
    }

    init {
        addItemType(ITEM_TYPE_LINE, object : OnMultiItemAdapterListener<GoodsInfoVO, DataBindingHolder<ItemGoodsLineLayoutBinding>> {

            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): DataBindingHolder<ItemGoodsLineLayoutBinding> {
                return DataBindingHolder(R.layout.item_goods_line_layout,parent)
            }

            override fun onBind(holder: DataBindingHolder<ItemGoodsLineLayoutBinding>, position: Int, item: GoodsInfoVO?) {
                // 绑定 item 数据
                if(item == null) return
                val mBinding  = holder.binding
//                mBinding.cover.loadImgUrl(item.goods_image,24)
                mBinding.cover.loadImgUrl("https://ts1.cn.mm.bing.net/th/id/R-C.b0089c802efa0179ae6f1a365473e2ed?rik=lfm%2bJ%2fspw5LX0w&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50079%2f2036.jpg_wh1200.jpg&ehk=qNQXICUl52BVrI2OOpTnCrI4wsTOPGByIeXp5MWc8nw%3d&risl=&pid=ImgRaw&r=0",24)
                mBinding.title.text = "${item.goods_name}"
                mBinding.price.text = (String.format(context.getString(R.string.currency), "${item?.goods_sku?.goods_price}"))

//                val layoutParams = mBinding.cover.layoutParams as ViewGroup.MarginLayoutParams
//                layoutParams.setMargins(SizeUtils.dp2px(8f),SizeUtils.dp2px(16f),0,SizeUtils.dp2px(16f))
//                mBinding.cover.layoutParams = layoutParams

            }

        }).addItemType(ITEM_TYPE_GRID, object : OnMultiItemAdapterListener<GoodsInfoVO, DataBindingHolder<ItemHomeGoodsBinding>> {

            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): DataBindingHolder<ItemHomeGoodsBinding> {
                return DataBindingHolder(R.layout.item_home_goods,parent)
            }

            override fun onBind(holder: DataBindingHolder<ItemHomeGoodsBinding>, position: Int, item: GoodsInfoVO?) {
                // 绑定 item 数据
                if(item == null) return
                var mBinding  = holder.binding
//                mBinding.cover.loadImgUrl(item.goods_image,24)
                mBinding.cover.loadImgUrl("https://ts1.cn.mm.bing.net/th/id/R-C.b0089c802efa0179ae6f1a365473e2ed?rik=lfm%2bJ%2fspw5LX0w&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50079%2f2036.jpg_wh1200.jpg&ehk=qNQXICUl52BVrI2OOpTnCrI4wsTOPGByIeXp5MWc8nw%3d&risl=&pid=ImgRaw&r=0",24)

                mBinding.title.text = "${item.goods_name}"
                mBinding.price.text = (String.format(context.getString(R.string.currency), "${item?.goods_sku?.goods_price}"))
            }

        }).onItemViewType { position, list ->
            if (list[position]?.itemType == 1) ITEM_TYPE_LINE
            else ITEM_TYPE_GRID
        }
    }

}